package com.lingshi.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping_common.entity.Permission;
import com.lingshi.shopping_common.result.BaseResult;
import com.lingshi.shopping_common.service.IPermissionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionServiceController {
    @DubboReference
    private IPermissionService permissionService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Permission permission){
        permissionService.add(permission);
        return BaseResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Permission permission){
        permissionService.update(permission);
        return BaseResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long pid){
        permissionService.delete(pid);
        return BaseResult.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/findById")
    public BaseResult findById(Long pid){
        permissionService.findById(pid);
        return BaseResult.success();
    }

    /**
     * 分页查询权限
     * @param page 页码
     * @param size 每页条数
     */
    @GetMapping("/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size){
        Page<Permission> search = permissionService.search(page, size);
        return BaseResult.success(search);
    }

    @GetMapping("/findAll")
    public BaseResult findAll(){
        List<Permission> permissions = permissionService.findAll();
        return BaseResult.success(permissions);
    }
}
